package com.zxt.recruit.util;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxt.recruit.model.SysElementRoles;
import com.zxt.recruit.model.SysElementUserRolesRef;
import com.zxt.recruit.service.impl.SysElementRolesServiceImpl;
import com.zxt.recruit.service.impl.SysElementUserRolesRefServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class UserRolesUtil {

    @Resource
    SysElementRolesServiceImpl rolesService;

    @Resource
    SysElementUserRolesRefServiceImpl rolesRefService;

    /**
     * 返回用户的角色列表
     * @param userId 用户id
     * @return
     */
    public List<String > userRolesGet(String  userId){
        List<SysElementRoles> rolesList = rolesService.list(new QueryWrapper<SysElementRoles>().eq("is_deleted", 0));
        List<SysElementUserRolesRef> userRolesRefs = rolesRefService.list(new QueryWrapper<SysElementUserRolesRef>()
                .eq("user_fk", userId)
        );
        List<String > list=new ArrayList<>();
        userRolesRefs.forEach(el->{
            rolesList.forEach(rl->{
                if(Objects.equals(rl.getRolesFk(), el.getRolesFk())){
                    list.add(rl.getName());
                }
            });
        });
        return list;
    }

}
